package com.woniuxy.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.Result;
import com.woniuxy.product.service.ProductService;
import com.woniuxy.product.service.dto.ProductDto;
import com.woniuxy.product.service.form.AddProductForm;
import com.woniuxy.product.service.form.UpdateProductStateForm;
import com.woniuxy.product.service.form.UserFindProductForm;
import com.woniuxy.product.service.param.UserFindProductParam;
import com.woniuxy.product.service.util.PageInfo;
import com.woniuxy.redis.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品接口")
public class ProductController {

    @Resource
    private ProductService productService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增物品
     * @param form
     * @return
     */
    @PostMapping("/addProduct")
    @ApiOperation("新增物品")
    @ApiImplicitParam(name = "form",value = "物品详情")
    public Result add(@RequestBody AddProductForm form) throws IOException {
        productService.add(form);
        return Result.fail(200,"新增物品成功");
    }

    /**
     * 查询物品
     * @param form
     * @return
     */
    @GetMapping("/userFindProduct")
    @ApiOperation("查询物品")
    @ApiImplicitParam(name = "form",value = "搜索条件")
    public Result userFindProductBy(UserFindProductForm form){
        PageInfo<ProductDto> pageInfo= productService.userFindProductByType(BeanUtil.toBean(form, UserFindProductParam.class));
        return Result.success("用户查询商品成功",pageInfo);
    }

    /**
     * 修改状态
     * @param form
     * @return
     */
    @PostMapping("/updateState")
    @ApiOperation("修改物品状态")
    @ApiImplicitParam(name = "form",value = "修改物品状态")
    public Result updateMovieState(@RequestBody UpdateProductStateForm form) throws IOException {

        productService.updateMovieState(form);

        return Result.fail(200,"修改影片状态成功");
    }

    /**
     * 添加物品到购物车
     * @param productId
     * @return
     */
    @ApiOperation("添加物品到购物车")
    @GetMapping("/addShopping")
    @ApiImplicitParams({@ApiImplicitParam(name = "productId",value = "物品ID"),
            @ApiImplicitParam(name = "token",value = "用户ID")
    })
    public Result addShopping(@RequestHeader String token,Integer productId)throws Exception{
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.getData(refreshToken,"id").toString());
        productService.addShopping(userId,productId);
        return Result.fail(200,"操作成功");
    }


    /**
     * 购物车中删除物品
     * @param productId
     * @return
     */
    @ApiOperation("购物车删除物品")
    @PostMapping("/delectShopping")
    @ApiImplicitParams({@ApiImplicitParam(name = "productId",value = "物品ID"),
            @ApiImplicitParam(name = "token",value = "用户ID")
    })
    public Result delectShopping(Integer productId,@RequestHeader String token) throws Exception{
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.getData(refreshToken,"id").toString());
        productService.delectShopping(productId,userId);
        return Result.fail(200,"操作成功");
    }




}

